package com.cyw.baselibrary.utils;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * 一个SharePrefence类，存储数据
 */
public class PreferencesUtils {

    public static final String NAME_EDGE_LEFT_OPERATION = "name_edge_left_operation";//保存左侧边缘菜单设置数据的sp名字
    public static final String NAME_EDGE_RIGHT_OPERATION = "name_edge_right_operation";//保存右侧边缘菜单设置数据的sp名字
    public static final String NAME_DEFAULT_CONFIG = "name_default_config";//保存app默认设置数据的sp名字
    public static final String NAME_APP_CONFIG = "name_app_config";//保存app设置数据的sp名字
    public static final String NAME_FLOAT_BALL_OPERATION = "name_float_ball_operation";//保存悬浮球菜单设置数据的sp名字
    private static PreferencesUtils mPref = null;
    private SharedPreferences mSharePrefer = null;
    private Editor mEditor = null;

    public void saveStringData(String key, String value) {
        mEditor.putString(key, value);
        mEditor.commit();
    }

    public String getStringData(String key) {
        return mSharePrefer.getString(key, "");
    }

    public void saveIntData(String key, int value) {
        mEditor.putInt(key, value);
        mEditor.commit();
    }

    public int getIntData(String key, int defaultValue) {
        return mSharePrefer.getInt(key, defaultValue);
    }

    public void saveLongData(String key, long value) {
        mEditor.putLong(key, value);
        mEditor.commit();
    }

    public long getLongData(String key, long defaultValue) {
        return mSharePrefer.getLong(key, defaultValue);
    }

    public void saveFloatData(String key, float value) {
        mEditor.putFloat(key, value);
        mEditor.commit();
    }

    public float getFloatData(String key) {
        return mSharePrefer.getFloat(key, 0);
    }

    public void saveBooleanData(String key, boolean value) {
        mEditor.putBoolean(key, value);
        mEditor.commit();
    }

    public boolean getBooleanData(String key) {
        return mSharePrefer.getBoolean(key, false);
    }

    public boolean getBooleanData(String key, boolean defValue) {
        return mSharePrefer.getBoolean(key, defValue);
    }


    public static PreferencesUtils getInstance(Context context, String spName) {
        mPref = new PreferencesUtils(context, spName);
        return mPref;
    }

    private PreferencesUtils(Context context, String spName) {
        mSharePrefer = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        mEditor = mSharePrefer.edit();
    }

    /**
     * 清空 SharedPreferences
     */
    public void clear() {
        mEditor.clear();
        mEditor.commit();
    }

}
